<!DOCTYPE html>
<html> 

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
	</head>

	<body>
		<script src="js/mui.js"></script>
		<script src="js/app.js"></script>
		<script type="text/javascript" charset="UTF-8">
			mui.init({  
        swipeBack:false  
})  
			//用户设备 1安卓，2ios
			function ismobile(test){
			    var u = navigator.userAgent, app = navigator.appVersion;
			    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
			     if(window.location.href.indexOf("?mobile")<0){
			      try{
			       if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
			        return '0'; 
			       }else{
			        return '1';
			       }
			      }catch(e){}
			     }
			    }else if( u.indexOf('iPad') > -1){
			        return '0';
			    }else{
			        return '1';
			    }
			};
			mui.plusReady(function() {
				//设置只支持竖屏幕显示
				
				plus.screen.lockOrientation("portrait-primary");
				//updaet
				plus.runtime.getProperty(plus.runtime.appid,function(inf){
				 // 当前版本
			     var wgtVersion = inf.version;
			    
			     var paradata={
			     	APPVERCODE : wgtVersion
			     }
			   
			     if(ismobile()==0){
							paradata.APPTYPE = 2; 
						}else{
							paradata.APPTYPE = 1;
						}
				 //alert(JSON.stringify(paradata))  
			     mui.post(Apiurl+'version/version' , paradata , function(data){ 
				     	// 如果有新版本，则提示需要更新
				     	//alert(data.update_info.APPVERCODE) 
			              if( data.update_info.APPVERCODE > wgtVersion ){ 
			                   mui.confirm('检查更新','发现新版本，是否更新',['确定','取消'],function(e){
			                      if(e.index==0){ 
			                          downloadWgt(); // 下载wgt方法  
			                      }else{
			                          return;
			                      } 
			                   })
			              }else{
			                  return;
			              }
			     }, 'json')
			  	});
	
				//localStorage.removeItem('user');
				if(!localStorage.$state) {
					//没有用户信息，跳转登陆页
					var loginWebView = plus.webview.getWebviewById("login");
					if(loginWebView) {
						loginWebView.show();
					} else {
						mui.openWindow({
							url: 'pages/login.html',
							id: 'login',
							preload: true,
							show: {
								aniShow: 'pop-in'
							},
							styles: {
								popGesture: 'hide'
							},
							waiting: {
								autoShow: false
							}
						});

					}
				} else {
					var state=app.getState();
					if(state.type==1){
						//二维码
						mui.openWindow({
							url: 'pages/ScanQRcode.html',
							id: 'ScanQRcode',
							preload: true,
							show: {
								aniShow: 'pop-in'
							},
							styles: {
								popGesture: 'hide'
							},
							waiting: {
								autoShow: false 
							}
						});
					}else{
						//二维码
						mui.openWindow({
							url: 'pages/main.html',
							id: 'main',
							preload: true,
							show: {
								aniShow: 'pop-in'
							},
							styles: {
								popGesture: 'hide'
							},
							waiting: {
								autoShow: false 
							}
						});
					}
					
				}
			});
			function downloadWgt (){
			   var that = this;
				// 更新文件 wgt 文件地址
				//测试地址
				//var wgtUrl = "http://tongban.weidu168.com/upload/files/tongban.wgt";
				
				//生产地址
				var wgtUrl = "http://tongban.dqbenxin.com/upload/files/tongban.wgt";
			    plus.nativeUI.showWaiting("正在更新...");  
			    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
			     if ( status == 200 ) {
			          console.log("下载wgt成功："+d.filename);
			          that.installWgt(d.filename); // 安装wgt方法
			      } else {
			          console.log("下载wgt失败！");
			          plus.nativeUI.alert("下载wgt失败！");
			      }
			      plus.nativeUI.closeWaiting();
			  }).start();
			}
			function installWgt(path) {
				  plus.nativeUI.showWaiting("安装wgt文件...");
				  plus.runtime.install(path,{},function(){
				      plus.nativeUI.closeWaiting();
				      console.log("安装wgt文件成功！");
				      plus.nativeUI.alert("应用资源更新完成！",function(){
				          plus.runtime.restart();
				      });
				  },function(e){ 
				      plus.nativeUI.closeWaiting();
				      console.log("安装wgt文件失败["+e.code+"]："+e.message);
				      plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);
				  });
			}
		</script>
	</body>

</html>